package cn.com.gridinfo.sooc.core.my.course;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.my.commen.CodeConst;
import cn.com.gridinfo.sooc.core.my.course.adapter.JRCourseAdapter;
import cn.com.gridinfo.sooc.core.my.course.bean.JRCourse;
import cn.com.gridinfo.sooc.core.my.dao.MyDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 我的课程
 * Created by liuxiaochen on 2015/12/8.
 */
public class MyCourseActivity extends BaseSoocActivity {

    @InjectView(R.id.listView)
    UltimateRecyclerView jiaru_recyclerView;//初始化listview
    @InjectView(R.id.tip)
    TextView tipBlank;//空白页提示
    MyDao dao;//我的 dao
    JRCourseAdapter adapter;//适配器
    LinearLayoutManager linearLayoutManager;//列表管理器
    int pageNo = 1;
    int pageSize = 10;
    int maxLastPositon;//数据最后位置
    private String title;//内容标题
    private String study;//确定是正在学习的课程，还是已经学完的课程  1：正在学的课程 2：已学完的课程

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_course);
        ButterKnife.inject(this);
        dao = new MyDao(this);
        linearLayoutManager = new LinearLayoutManager(this);
        jiaru_recyclerView.setLayoutManager(linearLayoutManager);
        if (getIntent().getStringExtra("from").equals("studing")) {
            title = "我在学课程";
            study = "2";
        } else {
            title = "已学完课程";
            study = "1";
            tipBlank.setText("还没有已学完课程哦~");
        }
        initView();
        dao.myAddCourse(pageNo, CodeConst.JRCOURSE_PUSHDOWN, study);
        showProgress(true);
    }

    //接口请求成功
    @Override
    public void onRequestSuccess(int requestCode) {
        showProgress(false);
        try {
            if (dao.status.equals("0")) {
                switch (requestCode) {
                    //课程刷新操作
                    case CodeConst.JRCOURSE_PUSHDOWN:
                        jiaru_recyclerView.setRefreshing(false);
                        if (dao.getJrCourseList() != null && !dao.getJrCourseList().isEmpty()) {
                            jiaru_recyclerView.hideEmptyView();
                        } else {
                            //无数据处理
                            jiaru_recyclerView.showEmptyView();
                        }
                        //刷新数据
                        adapter.refresh(dao.getJrCourseList());
                        if (dao.getJrCourseList().size() > (pageSize - 1))
                            //数据大于每页显示的条数，则重新开启加载更多
                            adapter.loadMoreDataView();
                        else {
                            //数据小于当前页的话则不加载显示更多
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                            linearLayoutManager.scrollToPosition(maxLastPositon);
                        }
                        break;
                    //课程翻页操作
                    case CodeConst.JRCOURSE_PULLUP:
                        if (dao.getJrCourseList() != null && !dao.getJrCourseList().isEmpty()) {
                            //加载更多
                            adapter.loadmore(dao.getJrCourseList());
                        } else {
                            //无数据则不显示下一页
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;

                }
            } else {
                MessageUtils.showImageShortToast(this, dao.statusCode);
            }

        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }

    }

    //初始化list数据 分页
    private void initView() {
        List<JRCourse> courseList = new ArrayList<>();
        adapter = new JRCourseAdapter(courseList, this);
        jiaru_recyclerView.setAdapter(adapter);
        jiaru_recyclerView.enableLoadmore();
        jiaru_recyclerView.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNo = 1;//首页
                dao.myAddCourse(pageNo, CodeConst.JRCOURSE_PUSHDOWN, study);
                linearLayoutManager.scrollToPosition(0);
            }

        });
        jiaru_recyclerView.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {
                if (itemsCount > (pageSize - 1)) {
                    maxLastPositon = maxLastVisiblePosition;
                    ++pageNo;
                    dao.myAddCourse(pageNo, CodeConst.JRCOURSE_PULLUP, study);
                } else {
                    adapter.loadNoDataView();
                    linearLayoutManager.scrollToPositionWithOffset(maxLastVisiblePosition, -1);
                }
            }
        });
        jiaru_recyclerView.hideEmptyView();
    }


    @Override
    public String setupToolBarTitle() {
        return title;
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void goBack() {
        finish();
    }
}